import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../utils/store.dart';
import '../model/book.dart';

class BookProvide with ChangeNotifier {
  static List<BookModel> bookInfo = [];
  static List<BookModel> boolList = [];
  //是否全选的按钮标志
  bool isAllCheck = true;

  /// @description: 目前无数据请求，测试
  /// @param {*}
  /// @return {*}
  static save(boolList) async {
    Store store = await Store.getInstance();
    boolList = json.encode(boolList);
    await store.setString(StoreKeys.booInfo, boolList);
  }

  getBookInfo() async {
    Store store = await Store.getInstance();
    var bookInfo = await store.getString(StoreKeys.booInfo);
    bookInfo = json.decode(bookInfo);
    for (int index = 0; index < bookInfo.length; index++) {
      boolList.add(BookModel.fromJson(bookInfo[index]));
    }
    notifyListeners();
  }

  //改变选中按钮的状态
  changeCheckBox(String bookId) async {
    Store store = await Store.getInstance();
    var bookInfo = await store.getString(StoreKeys.booInfo);
    bookInfo = json.decode(bookInfo);
    //遍历当前点击的item
    for (int index = 0; index < bookInfo.length; index++) {
      if (bookInfo[index]['bookId'] == bookId) {
        bookInfo[index]['ischeck'] = !bookInfo[index]['ischeck'];
        break;
      }
    }
    //重新持久化
    bookInfo = json.encode(bookInfo).toString();
    await store.setString(StoreKeys.booInfo, bookInfo);
    // await getBookInfo();
  }
}
